package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Option;
import com.tanhua.model.mongo.Questions;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjs
 * @version 1.0
 * @date 2022/1/9 0009 上午 9:12
 */
@DubboService
public class QuestionsApiImpl implements QuestionsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Questions> findByIds(List<String> questionIds) {
        List<ObjectId> ids = questionIds.stream().map(id -> new ObjectId(id)).collect(Collectors.toList());
        Query query = Query.query(Criteria.where("id").in(ids));
        return mongoTemplate.find(query, Questions.class);
    }

    @Override
    public void save(Questions questions) {
        mongoTemplate.save(questions);
    }

    @Override
    public List<Questions> findByQuestionnairesId(ObjectId id) {
        Query query = Query.query(Criteria.where("toQuestionnaireId").is(id));
        return mongoTemplate.find(query,Questions.class);
    }
}
